<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Product;
use App\Models\Shop\Category;
use App\Models\Shop\Brand;
use App\Http\Requests\StoreProduct;
use App\Http\Requests\UpdateProduct;
use App\Models\Shop\ProductGallery;
use DB;

class ProductController extends Controller
{
    function __construct()
    {
        view()->share([
            '_product'=>'am-active',
            '_shop'=>'am-in',


            'categories' => Category::with('children')->where('parent_id', 0)->
            orderBy('id', 'desc')->get(),
            'brands' => Brand::orderBy('id', 'desc')->get(),
            'filter_categories' => Category::filter_categories(),
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $products = Product::all_products($request);
        $infos = array('is_onsale', 'is_top', 'is_recommend', 'is_new','is_hot');

        return view('admin.shop.product.index', compact('products', 'infos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.product.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreProduct $request)
    {
        $product = Product::create($request->all());

        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $product->product_galleries()->create(['img' => $img]);
            }
        }
        $product->categories()->sync($request->category_id);
        return redirect(route('shop.product.index'))->with('success', '新增成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product = Product::with('brand', 'categories', 'product_galleries')->find($id);
        $p_categories = $product->categories->pluck('id');

        return view('admin.shop.product.edit', compact('product', 'p_categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateProduct $request, $id)
    {
        $product = Product::find($id);
        $product->update($request->all());
        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $product->product_galleries()->create(['img' => $img]);
            }
        }
        $product->categories()->sync($request->category_id);
        return redirect(route('shop.product.index'))->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

//        Product::destroy($id);
//        return redirect(route('shop.product.index'));
    }

    /***
     * 删除图片
     */
    public function delete_img(Request $request)
    {

        ProductGallery::destroy($request->id);
    }

    /***
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $product = Product::find($request->id);
        $attr = $request->attr;
        $product->$attr = !$product->$attr;
        $product->save();
    }

    /***
     * 改变库存
     */
    public function stock(Request $request)
    {
        $product = Product::find($request->id);
        $product->stock = $request->stock;
        $product->save();
    }

    /***
     * 改变单价
     */
    public function price(Request $request)
    {
        $product = Product::find($request->id);
        $product->price = $request->price;
        $product->save();
    }

    /***
     * 回收单个
     */
    public function recycle_one(Request $request)
    {
        Product::destroy($request->id);
    }

    /***
     * 回收多个
     */
    public function recycle_all(Request $request)
    {

        $checked_id = $request->checked_id;

        foreach ($checked_id as $id) {
            Product::destroy($id);
        }

    }



}
